﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using ACompany.OrderProcessing.Presentation;
using ACompany.OrderProcessing.Model;
using System.Collections.ObjectModel;

partial class CreateOrder : Page, ICreateOrderView
{
    private CreateOrderPresenter _Presenter;

    protected void Page_Init(object sender, System.EventArgs e)
    {
        _Presenter = new CreateOrderPresenter(this);
    }

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            _Presenter.Display();
        }
    }


    protected void btnAddToOrder_Click(object sender, EventArgs e)
    {
        _Presenter.AddProductBy(new Guid(this.ddlProducts.SelectedValue));
    }


    protected void btnPlaceOrder_Click(object sender, EventArgs e)
    {
        _Presenter.PlaceOrder();
    }

    public void ErrorMessage(string Message)
    {
        if (string.IsNullOrEmpty(Message))
        {
            pnlError.Visible = false;
        }
        else
        {
            pnlError.Visible = true;
        }
        this.litErrorMessage.Text = Message.Replace(Environment.NewLine, "<br/>");
    }

    public void InfoMessage(string Message)
    {
        if (string.IsNullOrEmpty(Message))
        {
            pnlInfo.Visible = false;
        }
        else
        {
            pnlInfo.Visible = true;
        }
        this.litInfoMessage.Text = Message.Replace(Environment.NewLine, "<br/>");
    }

    public void ViewTitle(string title)
    {
        this.litTitle.Text = title;
    }

    public List<Customer> Customers
    {
        set
        {
            this.ddlCustomers.DataSource = value;
            ddlCustomers.DataTextField = "Name";
            ddlCustomers.DataValueField = "Id";
            this.ddlCustomers.DataBind();
        }
    }

    public Order Order
    {
        get
        {
            object CurrentOrder = Session["CurrentOrder"];

            if (CurrentOrder == null)
            {
                CurrentOrder = new Order();

                Session["CurrentOrder"] = CurrentOrder;
            }

            return (Order)Session["CurrentOrder"];
        }
    }

    public List<Product> Products
    {
        set
        {
            this.ddlProducts.DataSource = value;
            this.ddlProducts.DataTextField = "Name";
            this.ddlProducts.DataValueField = "Id";
            this.ddlProducts.DataBind();
        }
    }


    public string City
    {
        set { this.litCity.Text = value; }
    }

    public string County
    {
        set { this.litCounty.Text = value; }
    }

    public string CustomerName
    {
        set { litCustomerName.Text = value; }
    }

    public string PostCode
    {
        set { this.litPostCode.Text = value; }
    }

    public string Street
    {
        set { this.litStreet.Text = value; }
    }

    public ReadOnlyCollection<Item> OrderItems
    {
        set
        {
            this.grdOrderItems.DataSource = value;
            this.grdOrderItems.DataBind();
        }
    }

    public void clearOrder()
    {
        Session["CurrentOrder"] = null;
        ResetSeletions();
    }

    private void ResetSeletions()
    {
        ddlCustomers.ClearSelection();
        ddlProducts.ClearSelection();
        grdOrderItems.DataSource = null;
        grdOrderItems.DataBind();

        litCustomerName.Text =
            litStreet.Text = litCounty.Text = litCity.Text = litPostCode.Text = string.Empty;
    }

    protected void btnSelectCustomer_Click(object sender, EventArgs e)
    {
        _Presenter.SetCustomerBy(new Guid(this.ddlCustomers.SelectedValue));
    }

    public bool SelectCustomerButtonEnabled
    {
        set { this.btnSelectCustomer.Enabled = value; }
    }

    public bool SelectProductButtonEnabled
    {
        set { this.btnAddToOrder.Enabled = value; }
    }

    public bool CreateOrderButtonEnabled
    {
        set { this.btnPlaceOrder.Enabled = value; }
    }



}